package com.hxh.generater.vue.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.entity.ProjectTableFiledConfig;
import com.hxh.entity.po.ClassInfo;
import com.hxh.entity.po.FieldName;
import com.hxh.util.GenUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater
 * @Author: HXH
 * @Description:
 * @Date: 2023/11/28 15:08
 */
public class GenSimpleApi {
    private static final String vueTemplatePathSuffix = "simple/template/";

    public static String generationApi(Project project, ClassInfo classInfo) throws Exception {
        String entityName = StringUtils.uncapitalize(classInfo.getClassName());
        boolean returnModel = false;
        if (project == null) {
            returnModel = true;
        }
        BufferedWriter writer;
        FileWriter fileWriter = null;
        StringWriter stringWriter = null;

        if (returnModel) {
            stringWriter = new StringWriter();
            writer = new BufferedWriter(stringWriter);
        } else {
            File file = GenUtil.newVueFile(project, "src\\api", entityName + ".js");
            fileWriter = new FileWriter(file);
            writer = new BufferedWriter(fileWriter);
        }

        StringBuilder processArrFieldToString = new StringBuilder();
        StringBuilder processStringFieldToArr = new StringBuilder();


        for (FieldName fieldName : classInfo.getFieldNameList()) {
            ProjectTableFiledConfig projectTableFiledConfig = fieldName.getProjectTableFiledConfig();
            if (projectTableFiledConfig == null) {
                continue;
            }
            if ("checkbox".equals(projectTableFiledConfig.getShowType())) {
                processArrFieldToString.append("    if (thisParams." + fieldName.getJavaName() + "Arr) {\n" +
                        "        thisParams." + fieldName.getJavaName() + " = params." + fieldName.getJavaName() + "Arr.toString()\n" +
                        "        thisParams." + fieldName.getJavaName() + "Arr = undefined\n" +
                        "    }\n");
                processStringFieldToArr.append("                    if (a." + fieldName.getJavaName() + " && a." + fieldName.getJavaName() + ".length > 0){\n" +
                        "                        a." + fieldName.getJavaName() + "Arr = a." + fieldName.getJavaName() + ".split(\",\")\n" +
                        "                    }\n");
            }
        }
        try {
            File template = new File(Constants.vueTemplatePath + vueTemplatePathSuffix + "replace\\api\\template");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#processArrFieldToString#", processArrFieldToString.toString());
                    line = line.replaceAll("#processStringFieldToArr#", processStringFieldToArr.toString());
                    line = line.replaceAll("#entityName#", entityName);
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            if (fileWriter != null) {
                fileWriter.close();
            }
            if (returnModel) {
                return stringWriter.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
